package com.mj.app.health.base;

import android.annotation.TargetApi;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.mj.app.health.MyApplication;
import com.mj.app.health.R;
import com.mj.app.health.defination.IntentKeys;
import com.mj.app.health.dialog.DialogLoading;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ========================================
 * *
 * title: BaseFragment
 * description:
 * *
 * Created by David Hogan on 2018/3/2.
 * ========================================
 */

public abstract class BaseFragment extends Fragment {

    protected View rootView;
    protected Unbinder unbinder;
    private DialogLoading dialogLoading;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if(rootView == null){
            rootView = inflater.inflate(setLayoutId(), container, false);
            unbinder = ButterKnife.bind(this, rootView);

            init();
        }else{
            ViewParent viewParent = rootView.getParent();
            if (viewParent != null && viewParent instanceof ViewGroup) {
                ((ViewGroup) viewParent).removeView(rootView);
            }
        }

        return rootView;
    }


    public void redirectTo(Class<? extends Activity> activity){
        redirectTo(activity, false);
    }

    public void redirectTo(Class<? extends Activity> activity, boolean isFinish){
        redirectTo(activity, isFinish, null);
    }

    public void redirectTo(Class<? extends Activity> activity, boolean isFinish, Bundle bundle){
        Intent intent = new Intent();
        intent.setClass(getActivity(),activity);
        if(bundle != null){
            intent.putExtra(IntentKeys.BUNDLE_KEY, bundle);
        }

        startActivity(intent);

        if(isFinish){
            getActivity().finish();
        }
    }

    protected void redirectToForResult(Class<? extends Activity> activity, int requestCode){

        redirectToForResult(activity, null, requestCode);
    }

    protected void redirectToForResult(Class<? extends Activity> activity, Bundle bundle, int requestCode){

        Intent intent = new Intent();
        intent.setClass(getActivity(),activity);
        if(bundle != null){
            intent.putExtra(IntentKeys.BUNDLE_KEY, bundle);
        }

        startActivityForResult(intent, requestCode, bundle);
    }

    public void setAnimationIn(){
        getActivity().overridePendingTransition(R.anim.left_slide_in, R.anim.left_slide_out);
    }

    protected void setAnimationOut(){
        getActivity().overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
    }

    @TargetApi(23)
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        onAttachToContext(context);
    }

    /*
     * Deprecated on API 23
     * Use onAttachToContext instead
     */
    @SuppressWarnings("deprecation")
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            onAttachToContext(activity);
        }
    }

    protected void onAttachToContext(Context context){}

    protected abstract int setLayoutId();
    protected abstract void init();


    public void showDialog(){

        if (getActivity().isFinishing()) return;
        if (dialogLoading == null)
            dialogLoading = new DialogLoading(getActivity(), getResources().getString(R.string.load_loading));

        dialogLoading.show();
    }


    public void hideLoading() {

        if (getActivity().isFinishing()) return;
        if (dialogLoading == null) return;

        dialogLoading.close();
    }
}
